<template>
	<gui-page :customHeader="false" :isHeaderSized="false" headerStyle="background-color:#F0F0F0" statusBarStyle="background-color:#F0F0F0">
		<view slot="gBody" class="" style="background-color: #f9f9f9;height: 100vh;">
			<view class="all">
				<view class="pic">
					<image src="../../static/menu_imgs/shui.png" mode="widthFix"></image>
				</view>
				<view class="title">
					{{title}}
				</view>
				<view v-if='kai' class="">
					<button @tap="start" type="primary">开始</button>
				</view>
				<view v-else class="gui-flex gui-row btn">
					<button @tap="que" type="primary">就是他了</button>
					<button @tap="start" type="default">再来一次</button>
				</view>
				
				<view class="desc">
					<text>解决世界难题，人生大困扰</text>
				</view>
				<view class="bottom gui-flex gui-rows gui-space-between">
					<view class="one gui-flex gui-columns gui-align-items-center" @tap="hang">
						<image src="../../static/menu_imgs/paihang.png" mode="widthFix"></image>
						<view class="zi">排行榜</view>
					</view>
					<view class="one gui-flex gui-columns gui-align-items-center" @tap="set">
						<image src="../../static/menu_imgs/zidingyi.png" mode="widthFix"></image>
						<view class="zi">自定义菜单</view>
					</view>
					<view class="one gui-flex gui-columns gui-align-items-center" @tap="sheng(1)">
						<image src="../../static/menu_imgs/shengji.png" mode="widthFix"></image>
						<view class="zi">升级</view>
					</view>
				</view>
			</view>
		</view>
	</gui-page>
</template>
<script>
	let graceJS = require('@/lizhili/js/grace.js');
	let common = require('@/common.js');
	//import popup from '@/components/DuDu-popup.vue'
	import autoUpdater from '@/components/autoUpdater.min.js';
	import {isOpen,openSqlite,closedb,executeSql,selectSql,isTable} from '@/database.js'
export default {
	data() { 
		return {
			mo:['盖浇饭','砂锅','大排档','米线','满汉全席','西餐','麻辣烫','自助餐','炒面','快餐','水果','西北风','馄饨','火锅','烧烤','泡面','速冻水饺','日本料理','涮羊肉','味千拉面','肯德基','面包','扬州炒饭','自助餐','海底捞','咖啡','比萨','麦当劳','兰州拉面','沙县小吃','烤鱼','海鲜','铁板烧','韩国料理','粥','快餐','东南亚菜','甜点','农家菜','川菜','包子','空气','麻辣烫','羊杂汤','炒饼','驴肉火烧','肉夹馍','不吃饭'],//默认添加数据库的文件
			kai:true,
			title:'今天吃什么?',
			data:{},
			is_ing:false,//是否正在进行
		}
	},
	onUnload(){
	    // 关闭数据库
	    //closedb()
	},
	async onShow(){
		openSqlite();	
	},
	onLoad() {
		this.sheng(0)
		this.init()
		uni.onAccelerometerChange((res) => {
			if (Math.abs(res.x) + Math.abs(res.y) + Math.abs(res.z) > 40 &&  !this.is_ing) {
				uni.vibrateLong({
				    success: function () {
				        console.log('success');
				    }
				});
				this.start()
			}
		})
	},
	methods: {
		async init(){
			await openSqlite();
			let res=await isTable('food');
			if(!res){
				//创建表
				await executeSql('CREATE TABLE IF NOT EXISTS `find` (' +
								'id INTEGER PRIMARY KEY AUTOINCREMENT,' + 'food_id int(11) DEFAULT NULL,' +
								'name varchar(255) DEFAULT NULL,' + 'time int(11) DEFAULT NULL);');
				await executeSql('CREATE TABLE IF NOT EXISTS `food` (' +
						'id INTEGER PRIMARY KEY AUTOINCREMENT,' + 'num int(11) DEFAULT 0,' +
						'name varchar(255) DEFAULT NULL);');
				//插入数据	
				let sql='insert into food (name) values';
				for(let v of this.mo){
					sql+='("'+v+'"),';
				}
				sql=sql.slice(0,sql.length-1)+';'
				await executeSql(sql)
			 }
		},
		async start(){
			this.kai=false
			this.is_ing=true
			//读取数据内容
			let food=await selectSql('SELECT * FROM food;')
			let arr =this.RandomNumBoth(food,20) //生成了 20条随机 吃的
			this.data=arr[arr.length-1]
			let ji=0
			let se=setInterval(()=>{
				if(ji==19){
					this.is_ing=false
					clearInterval(se)
				}
				this.title=arr[ji].name
				ji++
			},100)
		},
		RandomNumBoth(arr, maxNum) { //随机生成数组
			var numArr = [];
			var arrLength = arr.length;
			for (var i = 0; i < arrLength; i++) {
				var Rand = arr.length;
				//取出随机数 
				var number = Math.floor(Math.random() * arr.length); //生成随机数num
				numArr.push(arr[number]); //往新建的数组里面传入数值
				arr.splice(number, 1); //传入一个删除一个，避免重复
				if (arr.length <= arrLength - maxNum) {
					return numArr;
				}
			}
		},
		async que(){
			await openSqlite();
			//插入数据库
			await executeSql('UPDATE food SET num = num+1 WHERE id = '+this.data.id);
			await executeSql('insert into find (food_id,name,time) values ('+this.data.id+',"'+this.data.name+'","'+Date.parse(new Date())/1000+'");');
			uni.showToast({
				icon:'none',
				title:'恭喜您，找到自己喜欢吃的了！！！'
			})
		},
		hang(){
			uni.navigateTo({
				url:'hang'
			})
		},
		set(){
			uni.navigateTo({
				url:'set'
			})
		},
		sheng(lei){
			let that = this
			graceJS.post(
				common.apiServer, // + 'index/ajax'
				common.encrypted({
					lizhili: '0d89b868429be6158ba1ebc0f7c073de',
					type: 'update_app',
				}),
				'form', {
					token: ''
				},
				function(res) {
					if (res.code == 1) {
						if (res.app_v > plus.runtime.versionCode) {
							autoUpdater.init({
								packageUrl: res.url,
								content: res.desc,
								contentAlign: 'left',
								cancel: '取消该版本',
								cancelColor: '#007fff'
							});
							autoUpdater.show();
							
						}else{
							if(lei){
								uni.showToast({
									icon:'none',
									title:'最新版本无需升级'
								})
							}
						}
					}else{
						if(lei){
							uni.showToast({
								icon:'none',
								title:'最新版本无需升级'
							})
						}
					}
				},
				(e) => {
					if(lei){
						uni.showToast({
							icon:'none',
							title:'最新版本无需升级'
						})
					}
				}
			);
		}
	}
}
</script>
<style scoped>
	.all{
		padding: 0 30rpx;
		background-color: #f0f0f0;
		height: 100vh;
	}
	.title{
		font-size: 50rpx;
		text-align: center;
	}
	.pic{
		text-align: center;
	}
	.pic image{
		width: 50%;
	}
	.all button{
		margin-top: 50rpx;
	}
	.desc{
		text-align: center;
		margin-top: 30rpx;
	}
	.desc text{
		background: linear-gradient(to right, red, blue);
		-webkit-background-clip: text;
		color: transparent;
	}
	.bottom{
		margin-top: 20rpx;
	}
	.one{
		margin: 20rpx 0;
		padding: 30rpx 0;
		text-align: center;
		width: 30%;
		border-radius: 10rpx;
		background-color: #FFFFFF;
	}
	.one image{
		width:38% ;
	}
	.zi{
		margin-top: 10rpx;
		font-size: 28rpx;
	}
	.btn button{
		width: 45%;
		margin: 0 20rpx;
		margin-top: 50rpx;
	}
</style>
